package tms.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JToolBar;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.MenuListener;

import tms.Cache;
import tms.controler.BaseController;
import tms.controler.ChangeController;

public class WelcomeView extends JFrame {

    private static final long serialVersionUID = 1L;
    public static final String MENU_FILE = "File";
    public static final String MENU_ITEM_LOGIN = "Login/Register";
    public static final String MENU_ITEM_LOGOUT = "Logout";
    public static final String MENU_ITEM_QUIT = "Exit";
    public static final String DIALOG_QUIT_MSG = "Do you really want to exit ?";
    public static final String DIALOG_QUIT_TITLE = "Exit ?";

    private JPanel dynamicPanel;

    private BaseController controller;

    private JMenuItem mnuItemLogin;
    private JMenu mnuUser;
    private JMenuItem mnuLogout;
    private JButton btnAccMag;
    private JButton btnChgTicket;
    private JButton btnHistoryBooking;

    public WelcomeView() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(900, 700));
        pack();

        // center frame
        Tools.centerFrame(this);

        controller = BaseController.getInstance();

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu menu = new JMenu(MENU_FILE);
        menuBar.add(menu);

        // menu login
        mnuItemLogin = createMenuItem(menu, MENU_ITEM_LOGIN, new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                addPanel(new LoginView(), true, 580, 300, 200, 200, "Login");
            }
        });

        menuBar.add(Box.createHorizontalGlue());

        mnuUser = createMenu(menuBar, "", null);

        // menu logout
        mnuLogout = createMenuItem(mnuUser, MENU_ITEM_LOGOUT, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                controller.logout();
            }
        });
        mnuLogout.setVisible(false);

        createMenuSeparator(menu);

        // menu quit
        createMenuItem(menu, MENU_ITEM_QUIT, new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                quit();
            }
        });

        JToolBar toolbar = new JToolBar();
        toolbar.setLayout(new GridLayout());
        JButton btnBooking = new JButton("Booking");
        btnBooking.setFont(Tools.TAHOMA_BOLD_12);
        toolbar.add(btnBooking);
        btnBooking.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                controller.showSearchFlightView();
            }
        });

        btnChgTicket = new JButton ("Change Ticket");
        btnChgTicket.setFont(Tools.TAHOMA_BOLD_12);
        toolbar.add(btnChgTicket);
        btnChgTicket.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ChangeController changeCtrl = new ChangeController();
                changeCtrl.createView();
                ((ChangeView) changeCtrl.getView()).setVisible(true);
            }
        });

        btnHistoryBooking = new JButton ("History Booking");
        btnHistoryBooking.setFont(Tools.TAHOMA_BOLD_12);
        toolbar.add(btnHistoryBooking);
        btnHistoryBooking.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if (Cache.currentUser == null) {
                    addPanel(new LoginView(), true, 580, 300, 200, 200, "Login");
                } else {
                    addPanel(new HistoryView(Cache.currentUser), false, 600, 600, 100, 100, "History");
                }
            }
        });

        btnAccMag = new JButton("Account");
        btnAccMag.setFont(Tools.TAHOMA_BOLD_12);
        toolbar.add(btnAccMag);
        btnAccMag.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new RegisterView(Cache.currentUser, true).setVisible(true);
            }
        });
        btnAccMag.setEnabled(false);

        Container content = getContentPane();
        content.add(toolbar, BorderLayout.NORTH);
        dynamicPanel = new JPanel();

        JScrollPane scrollpane = new JScrollPane(dynamicPanel);
        scrollpane.getVerticalScrollBar().setUnitIncrement(16);
        scrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(scrollpane, BorderLayout.CENTER);
    }

    public void setMenuUserCaption(String caption) {
        mnuUser.setText(caption);
    }

    public JPanel getDynamicPanel() {
        return dynamicPanel;
    }

    /**
     * User has logged in.
     */
    public void hideLoginStuffs() {
        mnuItemLogin.setVisible(false);
        btnAccMag.setEnabled(true);
        mnuLogout.setVisible(true);
    }

    /**
     * User has logged out.
     */
    public void hideLogoutStuffs() {
        mnuItemLogin.setVisible(true);
        btnAccMag.setEnabled(false);
        mnuLogout.setVisible(false);
    }

    private void addPanel(JPanel p, boolean isModal, int width, int height,
            int x, int y, String title) {
        JDialog dialog = new JDialog(this, isModal);
        dialog.setTitle(title);
        dialog.add(p);
        dialog.setPreferredSize(new Dimension(width, height));
        int centerX = this.getX() + this.getWidth() / 2 - width / 2;
        int centerY = this.getY() + this.getHeight() / 2 - height / 2;
        dialog.setLocation(centerX, centerY);
        dialog.pack();
        dialog.setVisible(true);
    }

    public void quit() {
        int answer = JOptionPane.showConfirmDialog(null, DIALOG_QUIT_MSG,
                DIALOG_QUIT_TITLE, JOptionPane.YES_NO_OPTION);
        if (answer == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }

    private JMenu createMenu(JMenuBar bar, String caption, MenuListener listener) {
        JMenu menu = new JMenu(caption);
        menu.addMenuListener(listener);
        bar.add(menu);
        return menu;
    }

    private JMenuItem createMenuItem(JMenu menu, String name, ActionListener action) {
        JMenuItem menuItem = new JMenuItem(name);
        menuItem.addActionListener(action);
        menu.add(menuItem);
        return menuItem;
    }

    private void createMenuSeparator(JMenu menu) {
        JSeparator separator = new JSeparator();
        separator.setForeground(Color.lightGray);
        menu.add(separator);
    }
}
